package com.anolesoft.twelvePlan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.anolesoft.twelvePlan.dto.MesPlanGradeDTO;
import com.anolesoft.twelvePlan.entity.MesPlanGradeDO;
import com.anolesoft.twelvePlan.mapper.MesPlanGradeMapper;
import com.anolesoft.twelvePlan.service.MesPlanGradeService;
import com.anolesoft.yr.util.CommonUtils;
import com.anolesoft.yr.util.PageData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zyq
 * @date 2022/5/28
 */
@Service
@Slf4j
public class MesPlanGradeServiceImpl implements MesPlanGradeService {
    @Autowired
    private MesPlanGradeMapper mesPlanGradeMapper;

    @Override
    public PageData<List<MesPlanGradeDTO>> queryPlanGrade(MesPlanGradeDTO mesPlanGradeDTO, Integer pageNo, Integer pageSize) {
        Page<MesPlanGradeDO> page = new Page<>(pageNo, pageSize);
        List<MesPlanGradeDO> mesPlanGradeDOS = this.mesPlanGradeMapper.queryPlans(MesPlanGradeDO.builder()
                .planGrade(CommonUtils.stringNull(mesPlanGradeDTO.getPlanGrade()))
                .factoryId(CommonUtils.stringNull(mesPlanGradeDTO.getFactoryId()))
                .build(), page);
        List<MesPlanGradeDTO> mesPlanGradeDTOS = mesPlanGradeDOS.stream()
                .map(t -> BeanUtil.copyProperties(t, MesPlanGradeDTO.class))
                .collect(Collectors.toList());
        return new PageData<>(((int) page.getTotal()), mesPlanGradeDTOS);
    }

    @Override
    public String insertPlanGrade(MesPlanGradeDTO mesPlanGradeDTO) {
        String result;
        MesPlanGradeDO mesPlanGradeDO = MesPlanGradeDO.builder()
                .planGrade(CommonUtils.stringNull(mesPlanGradeDTO.getPlanGrade()))
                .factoryId(CommonUtils.stringNull(mesPlanGradeDTO.getFactoryId()))
                .createId(CommonUtils.stringNull(mesPlanGradeDTO.getCreateId()))
                .build();
        int repeat = this.mesPlanGradeMapper.repeatPlanGrade(mesPlanGradeDO);
        log.debug("查重结果:{}" + repeat);
        if (repeat == 0) {
            this.mesPlanGradeMapper.insertPlanGrade(mesPlanGradeDO);
            result = "success";
        } else {
            result = "repeat";
        }
        return result;
    }

    @Override
    public void deletePlanGrade(String objuid) {
        this.mesPlanGradeMapper.deletePlanGrade(objuid);
    }
}
